package com.itht.baoleme.config;

import com.itht.baoleme.domain.web.WebCodeEnum;
import com.itht.baoleme.domain.web.WebServiceException;
import com.itht.baoleme.domain.web.WebServiceMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 异常全局捕获
 */
@RestControllerAdvice
@Slf4j
public class ControllerExceptionAdvice {

    /**
     * 全局捕获空指针异常，进行统一输出
     * @param e 异常
     * @return message
     */
    @ExceptionHandler(NullPointerException.class)
    public WebServiceMessage nullPointerExceptionHandler(NullPointerException e) {
        log.error("发生空指针异常了！", e);
        return WebServiceMessage.error("程序空指针异常");
    }

    /**
     * 全局捕获自定义异常，进行统一输出
     * @param e 异常
     * @return message
     */
    @ExceptionHandler(WebServiceException.class)
    public WebServiceMessage webServiceExceptionHandler(WebServiceException e) {
        log.error("警告！！！程序出现异常", e);
        return e.getWebMessage();
    }

    /**
     * 处理上面没有指定的异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public WebServiceMessage exceptionHandler(Exception e) {
        log.error("警告！！！程序出现异常", e);
        return WebServiceMessage.error(WebCodeEnum.ERROR);
    }

}
